package au.edu.anu.club.cs;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import au.com.bytecode.opencsv.CSVReader;
import au.edu.anu.club.cs.ft.FTClient;
import au.edu.anu.club.cs.sql.TeamDataSource;
import au.edu.anu.club.cs.sql.TeamLocationDataSource;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class ANUIBActivity extends MapActivity {
	
	private MapView mapView; 
	private MapController mc;
	private GeoPoint locIB;
	private FTClient ft;
	
	private TeamDataSource teamDS;
	private TeamLocationDataSource teamLocationDS;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		teamDS = new TeamDataSource(this);
		teamDS.open();
		teamLocationDS = new TeamLocationDataSource(this);
		teamLocationDS.open();
		
		
		
		MapView mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		
        mc = mapView.getController();
        
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        
        ft = new FTClient(); // Do not need authentication token.
        String test = ft.getUpdates();
        CSVReader reader = new CSVReader(new StringReader(test));
        try {
			List<String[]> entries = reader.readAll();
			for (String[] entry : entries) {
				Log.e("REQUEST HANDLER", "DERP " + entry[0] + " | " + entry[1] + " | " + entry[2] + " | " + entry[3]);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
        
        // Hard coded location of IB.
        String coordinates[] = {"-35.275124", "149.120451"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);
        
        locIB = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
        mc.animateTo(locIB);
        mc.setZoom(17); 
        mapView.invalidate();
        
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
        ANUIBItemizedOverlay itemizedOverlay = new ANUIBItemizedOverlay(drawable, this);
        OverlayItem overlayItem = new OverlayItem(locIB, "Sup", "ANU");
        itemizedOverlay.addOverlay(overlayItem);
        mapOverlays.add(itemizedOverlay);
        
    }
    
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	protected void onResume() {
		teamDS.open();
		teamLocationDS.open();
		super .onResume();
	}
	
	@Override
	protected void onPause() {
		teamDS.close();
		teamLocationDS.close();
		super .onPause();
	}
}